package com.xxx.video.dynamic.presenters;

import android.content.Context;

import androidx.annotation.NonNull;

import com.xxx.video.commen.beans.BaseBean;
import com.xxx.video.commen.presenters.BasePresenter;
import com.xxx.video.commen.utils.ContactUtils;
import com.xxx.video.commen.utils.ListUtils;
import com.xxx.video.commen.utils.ResponseUtils;
import com.xxx.video.commen.views.AddFriendExtraMessageDialog;
import com.xxx.video.dynamic.beans.AddContactsFriendBean;
import com.xxx.video.dynamic.beans.ContactBean;
import com.xxx.video.network.listeners.CommonResponseListener;
import com.xxx.video.network.utils.NetWorkUtils;

import java.util.ArrayList;
import java.util.List;

public class AddContactsFriendsPresenter extends BasePresenter<IAddContactsFriendsView> {

    public AddContactsFriendsPresenter(@NonNull Context context, IAddContactsFriendsView baseView) {
        super(context, baseView);
    }

    public void getContactFriendList() {
        List<ContactBean> contactBeanList = ContactUtils.getInstance().getContactList(context);
        if (!ListUtils.isEmpty(contactBeanList)) {
            List<String> contactBeanList2 = new ArrayList<>();
            for (ContactBean contactBean : contactBeanList) {
                contactBeanList2.add(contactBean.phone);
            }
            String[] phoneList = new String[contactBeanList.size()];
            contactBeanList2.toArray(phoneList);
            NetWorkUtils.getInstance().mailList(phoneList, new CommonResponseListener<AddContactsFriendBean>() {
                @Override
                public void onSuccess(AddContactsFriendBean successOb, String result) {
                    if (ResponseUtils.getInstance().isNoError(successOb)) {
                        if (getBaseView() != null) {
                            getBaseView().initData2View(successOb);
                        }
                    }
                }

                @Override
                public void onFailure(Object failureObj) {
                    if (getBaseView() != null) {
                        getBaseView().showRootLayoutStatus(3);
                    }
                }
            });
        }
    }

    /**
     * add friend
     *
     * @param listBean data
     * @param position position
     */
    public void addFriends(AddContactsFriendBean.ListBean listBean, int position) {
        AddFriendExtraMessageDialog addFriendExtraMessageDialog = new AddFriendExtraMessageDialog(context);
        addFriendExtraMessageDialog.show();
        addFriendExtraMessageDialog.setOnButtonClickListener(message -> NetWorkUtils.getInstance().addFriends(listBean.userId, listBean.username, listBean.nickName, listBean.picUrl, message, new CommonResponseListener<BaseBean>() {
            @Override
            public void onSuccess(BaseBean successOb, String result) {
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        getBaseView().addFriendSuccess(position);
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {

            }
        }));

    }
}
